﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace DSLFactory.VSCTDesigner
{
    class IconIdEditor : UITypeEditor
    {
        private IWindowsFormsEditorService _edService;

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            BinaryLinkShape shape = context.Instance as BinaryLinkShape;
            CommandHasIcon cmd = shape.ModelElement as CommandHasIcon;

            string[] values = cmd.Bitmap.usedList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            _edService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            ListBox list = new ListBox();
            list.Click += new EventHandler(list_Click);
            foreach( var id in values )
            {
                list.Items.Add(id.Trim());
            }

            _edService.DropDownControl(list);
            if( list.SelectedIndex >= 0)
            {
                string val = (string)list.Items[list.SelectedIndex];
                return val;
            }
            return base.EditValue(context, provider, value);
        }

        void list_Click(object sender, EventArgs e)
        {
            _edService.CloseDropDown();
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
    }
}
